package util

import (
	"bytes"
	"encoding/json"
	"errors"
	"github.com/wenzhenxi/gorsa"
	"golang.org/x/crypto/bcrypt"
	"io/ioutil"
	"net/http"
	"regexp"
)

func Password(pwd []byte) (string, error) {
	hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)
	if err != nil {
		return "", errors.New("密码错误")
	}
	return string(hash), nil
}

/**
判断手机号码格式
*/
func IsPhone(strPhone string) bool {
	regular := "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9])|(19[0-9])|(17[0-9]))\\d{8}$"
	reg := regexp.MustCompile(regular)
	return reg.MatchString(strPhone)
}

func Encrypt(data string) (string, error) {
	var publicKey = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAZ3qXoX1uvrvUOXU1sI7kpZc85AfT0vtb2AzUp+LyZyOyC0U2FNDrdfGkzAUZidIqt0aVRHvEyFyV4ttsubiLG/fF40qDmtBgLySSGJcXwbjEM0f3ZDDLAbejsncuXOQKxytHUCxJ6XlffnEiBfbs+8BzDpWssdfpIVQ5wUWEnQIDAQAB
-----END PUBLIC KEY-----
`
	res, err := gorsa.PublicEncrypt(data, publicKey)
	if err != nil {
		return "", err
	}
	return res, nil
}

func Decrypt(data string) string {
	var publicKey = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCAZ3qXoX1uvrvUOXU1sI7kpZc85AfT0vtb2AzUp+LyZyOyC0U2FNDrdfGkzAUZidIqt0aVRHvEyFyV4ttsubiLG/fF40qDmtBgLySSGJcXwbjEM0f3ZDDLAbejsncuXOQKxytHUCxJ6XlffnEiBfbs+8BzDpWssdfpIVQ5wUWEnQIDAQAB
-----END PUBLIC KEY-----
`
	res, err := gorsa.PublicDecrypt(data, publicKey)
	if err != nil {
		return ""
	}
	return res
}

/**
get请求
*/
func HttpGet(url string, params map[string]string, headers map[string]string) (string, error) {
	req, err := http.NewRequest("GET", url, nil)

	if err != nil {
		return "", errors.New("request is error：" + err.Error())
	}
	// 请求头
	if headers != nil {
		for k, v := range headers {
			req.Header.Add(k, v)
		}
	}
	// 请求参数
	q := req.URL.Query()
	if params != nil {
		for k, v := range params {
			q.Add(k, v)
		}
		req.URL.RawQuery = q.Encode()
	}

	client := &http.Client{}
	res, errClient := client.Do(req)
	if errClient != nil {
		return "", err
	}
	// 关闭body
	defer res.Body.Close()

	body, errBody := ioutil.ReadAll(res.Body)
	if errBody != nil {
		return "", err
	}

	return string(body), nil
}

func HttpPost(url string, body map[string]interface{}, params map[string]string, headers map[string]string) (string, error) {
	var bodyJson []byte
	if body != nil {
		var err error
		bodyJson, err = json.Marshal(body)
		if err != nil {
			return "", errors.New("body error: " + err.Error())
		}
	}

	req, errReq := http.NewRequest("POST", url, bytes.NewReader(bodyJson))
	if errReq != nil {
		return "", errors.New("request error: " + errReq.Error())
	}
	// 请求头
	if headers != nil {
		for k, v := range headers {
			req.Header.Add(k, v)
		}
	}
	// 请求参数
	if params != nil {
		q := req.URL.Query()
		for k, v := range params {
			q.Add(k, v)
		}
		req.URL.RawQuery = q.Encode()
	}

	client := &http.Client{}

	res, errRes := client.Do(req)
	if errRes != nil {
		return "", errRes
	}
	defer res.Body.Close()

	resBody, errBody := ioutil.ReadAll(res.Body)
	if errBody != nil {
		return "", errBody
	}
	return string(resBody), nil
}
